package com.neusoft.bizcore.web.dto.result;

import java.util.List;

import org.springframework.data.domain.Page;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.neusoft.bizcore.web.exception.UnitedException;

import io.swagger.annotations.ApiModelProperty;

/**
 * the result DTO of paged request
 *
 * @author sunchf
 * @date 2018年12月10日 上午9:48:47
 */
public class PageResultDTO<T> extends ResultListDTO<T> {

    private static final long serialVersionUID = -9074923744912103235L;

    /**
     * page detail
     */
    @ApiModelProperty(value = "page detail", position = 2)
    private PageDetail pageable;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty(value = "data", index = 2)
    @Override
    public List<T> getData() {
        return super.getData();
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty(value = "pageable", index = 3)
    public PageDetail getPageable() {
        return this.pageable;
    }

    public void setPageable(final PageDetail pageable) {
        this.pageable = pageable;
    }

    public PageResultDTO() {
    }

    PageResultDTO(final Status status) {
        super(status);
    }

    public static <T> PageResultDTO<T> success(final Page<T> page) {
        if (page == null) {
            throw new UnitedException("NullPointerException. %s",
                    "The formal parameter 'com.neusoft.bizcore.web.dto.result.PageDetail' cannot be null");
        }

        final PageResultDTO<T> result = new PageResultDTO<>(Status.success);
        result.setData(page.getContent());
        result.setPageable(PageDetail.convert(page));
        return result;
    }

    public static <T> PageResultDTO<T> success(final List<T> listData, final Page<?> page) {
        if (page == null) {
            throw new UnitedException("NullPointerException. %s",
                    "The formal parameter 'com.neusoft.bizcore.web.dto.result.PageDetail' cannot be null");
        }

        final PageResultDTO<T> result = new PageResultDTO<>(Status.success);
        result.setData(listData);
        result.setPageable(PageDetail.convert(page));
        return result;
    }

}
